package com.project.modules.test.controller;


import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.common.utils.ResponseParam;
import com.project.modules.sys.controller.AbstractController;
import com.project.modules.test.entity.RegisterInfoEntity;
import com.project.modules.test.service.RegisterInfoService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 系统用户
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年10月31日 上午10:40:10
 */
@RestController
@RequestMapping("/main/vue")
public class MainController extends AbstractController {
	@Autowired
	private RegisterInfoService registerInfoService;

	/**
	 * 所有用户列表
	 */
	@PostMapping("/list")
	@RequiresPermissions("main:vue:list")
	public ResponseParam list(@RequestParam(required = true, defaultValue = "1") Integer page,
							  @RequestParam(required = false, defaultValue = "10") Integer pageSize){
		ResponseParam responseParam = ResponseParam.success();

//		int page = bodyMap.containsKey("page") ? (int) bodyMap.get("page") : 0;
//		int pageSize = bodyMap.containsKey("pageSize") ? (int) bodyMap.get("pageSize") : 10;

		PageHelper.startPage(page, pageSize);
		List<RegisterInfoEntity> list = registerInfoService.queryByPage();

		//获取匹配到的第一个元素
		RegisterInfoEntity register = list.stream().filter(lists -> StringUtils.equals(lists.getUserName(), "11"))
				.findFirst().get();
		// 获取匹配到的所有元素集合
		List<RegisterInfoEntity> listAll = list.stream().filter(lists -> StringUtils.equals(lists.getUserName(), "11"))
				.collect(Collectors.toList());
		// 将集合中按照某个值就行分组生成map对象
		Map<String, List<RegisterInfoEntity>> registerMap = list.stream().collect(Collectors.groupingBy(RegisterInfoEntity::getExamName));

		PageInfo<RegisterInfoEntity> p = new PageInfo<>(list);

		list = p.getList();

		responseParam.addParam("data", JSONArray.toJSON(list).toString());

		responseParam.initPageParam(p);

		return responseParam.addMessage("success");
	}
}
